﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Configuration;

namespace MSCRMSendReportInEmail
{
    public class ProgramUtil
    {
        public readonly string rDataBaseName = ConfigurationSettings.AppSettings["DataBaseName"];
        public readonly string rReportFullPath = ConfigurationSettings.AppSettings["ReportFullPath"];

        public string FileName { get; set; }
        public string AttachmentSubject { get; set; }
        public string ReportName { get; set; }
        public string ReportFormat { get; set; }
        public string To { get; set; }
        public string FromEntityLogicalName { get; set; }
        public string FromEntityId { get; set; }
        public string EmailTemplateTitle { get; set; }
        public string XmlPath { get; set; }
        
        public string ReportFullPath
        {
            get
            {
                return String.Format(rReportFullPath, rDataBaseName, ReportName);
            }
        }
        /// <summary>
        /// gets the xml file path from parameter and sets the data members from the xml
        /// </summary>
        /// <param name="args"></param>
        public void GetParameters(string[] args)
        {
            if (args.Length < 1)
            {
                throw new MissingFieldException("Missing configuration xml in program arguments");
            }
            else
            {
                XmlPath = args[0];
                XDocument xdoc = null;
                try
                {
                    xdoc = XDocument.Load(XmlPath);
                }
                catch(Exception ex)
                {
                    throw new Exception(String.Format("Failed to open XML: {0}", ex.ToString()));
                }
                var data = from item in xdoc.Descendants("configuration")
                           select new
                           {
                               report_format = item.Element("report").Element("format").Value,
                               report_name = item.Element("report").Element("reportname").Value,
                               attachment_subject = item.Element("attachment").Element("subject").Value,
                               file_name = item.Element("attachment").Element("filename").Value,
                               template_title = item.Element("email").Element("template_title").Value,
                               to = item.Element("email").Element("to").Value,
                               from_entity_id = item.Element("email").Element("from").Element("id").Value,
                               from_entity_logical_name = item.Element("email").Element("from").Element("entityLogicalName").Value
                           };
                //reports
                ReportFormat = data.First().report_format;
                ReportName = data.First().report_name;
                //attachments
                FileName = data.First().file_name;
                AttachmentSubject = data.First().attachment_subject;
                //email
                EmailTemplateTitle = data.First().template_title;
                To = data.First().to;
                FromEntityId = data.First().from_entity_id;
                FromEntityLogicalName = data.First().from_entity_logical_name;
            }
        }
    }
}
